package canss.webservice.controls;

import org.apache.beehive.controls.api.bean.Control;
import org.apache.beehive.controls.api.bean.ControlImplementation;

import canss.encoder.DBStringEncoder;
import canss.webservice.model.Channel;

import weblogic.jdbc.SQLExceptionWrapper;

import java.io.Serializable;

@ControlImplementation
public class ChannelControlImpl implements ChannelControl, Serializable
{
	private static final long serialVersionUID = 7800675793393748280L;

	@Control
	private BusinessSystemDB businesssystemDB;
	
	private final void DB2Server(Channel channel)
	{
		channel.setCHANNELNAME(DBStringEncoder.DB2Server(channel.getCHANNELNAME()));
		channel.setOUTBOUND_RULEID(DBStringEncoder.DB2Server(channel.getOUTBOUND_RULEID()));
		channel.setTBLPID_PID(DBStringEncoder.DB2Server(channel.getTBLPID_PID()));
	}

	public Channel[] getEntireChannel() throws SQLExceptionWrapper
	{
		Channel[] ret = businesssystemDB.getEntireChannel();
		if(ret==null)
			return null;
		for(Channel channel : ret)
			DB2Server(channel);
		return ret;
	}
	
	public Channel[] getChannelByPID(String _pid) throws SQLExceptionWrapper
	{
		Channel[] ret = businesssystemDB.getChannelByPID(DBStringEncoder.Server2DB(_pid));
		if(ret==null)
			return null;
		for(Channel channel : ret)
			DB2Server(channel);
		return ret;
	}
	
	public Channel[] getChannelByPIDChannelname(String _pid, String _channelname) throws SQLExceptionWrapper
	{
		Channel[] ret = businesssystemDB.getChannelByPIDChannelname(DBStringEncoder.Server2DB(_pid), DBStringEncoder.Server2DB(_channelname));
		if(ret==null)
			return null;
		for(Channel channel : ret)
			DB2Server(channel);
		return ret;
	}
	
	public Boolean addNewChannel(String _tblPID_pid, String _channelname) throws SQLExceptionWrapper
	{
		int ret = businesssystemDB.addNewChannel(DBStringEncoder.Server2DB(_tblPID_pid), DBStringEncoder.Server2DB(_channelname));
		return ret>0;
	}
	
	public Boolean setChannelRuleByPIDIndex(String _tblPID_pid, Integer _index_2, String _outbound_ruleid) throws SQLExceptionWrapper
	{
		int ret = businesssystemDB.setChannelRuleByPIDIndex(DBStringEncoder.Server2DB(_tblPID_pid), _index_2, DBStringEncoder.Server2DB(_outbound_ruleid));
		return ret>0;
	}
	
	public Boolean clearChannelRuleByPIDIndex(String _tblPID_pid, Integer _index_2) throws SQLExceptionWrapper
	{
		int ret = businesssystemDB.clearChannelRuleByPIDIndex(DBStringEncoder.Server2DB(_tblPID_pid), _index_2);
		return ret>0;
	}
	
	public Boolean renameChannelName(String _tblPID_pid, Integer _index_2, String _channelname) throws SQLExceptionWrapper
	{
		int ret = businesssystemDB.renameChannelName(DBStringEncoder.Server2DB(_tblPID_pid), _index_2, DBStringEncoder.Server2DB(_channelname));
		return ret>0;
	}
	
	public Boolean removeChannelByPIDIndex(String _tblPID_pid, Integer _index_2) throws SQLExceptionWrapper
	{
		int ret = businesssystemDB.removeChannelByPIDIndex(DBStringEncoder.Server2DB(_tblPID_pid), _index_2);
		return ret>0;
	}
}